package edu.arhs.team1100.gamename.forms;

import edu.arhs.team1100.gamename.handlers.EventHandler;
import edu.arhs.team1100.gamename.handlers.EventMatchHandler;
import edu.arhs.team1100.gamename.handlers.TeamEventMatchHandler;
import edu.arhs.team1100.gamename.handlers.TeamHandler;
import edu.arhs.team1100.gamename.objects.EventMatch;
import edu.arhs.team1100.gamename.objects.Team;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.NoRouteToHostException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Eddie
 */
public class ClientApplication extends javax.swing.JFrame {

    /**
     * Creates new form ClientForm
     */
    public ClientApplication() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar = new javax.swing.JMenuBar();
        teamMenuItem = new javax.swing.JMenu();
        jMenu1 = new javax.swing.JMenu();
        teamBlueAllianceMenuItem = new javax.swing.JMenuItem();
        teamEventMenuItem = new javax.swing.JMenuItem();
        teamManualMenuItem = new javax.swing.JMenuItem();
        teamViewMenuItem = new javax.swing.JMenuItem();
        eventMenu = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        eventBlueAllianceMenuItem = new javax.swing.JMenuItem();
        eventManualMenuItem = new javax.swing.JMenuItem();
        eventViewAllMenuTiem = new javax.swing.JMenuItem();
        teamMatchMenu = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        matchManualMenuItem = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        matchesAllMenuItem = new javax.swing.JMenuItem();
        matchByTeamMenuItem = new javax.swing.JMenuItem();
        matchByEventMenuItem = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        eventMatchMenu = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        eventMatchBlueAllianceMenuItem = new javax.swing.JMenuItem();
        eventMatchManualMenuItem = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        eventMatchAllMenuItem = new javax.swing.JMenuItem();
        eventMatchByEventMenuItem = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Scouting Client 2015");

        menuBar.setBorder(null);

        teamMenuItem.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        teamMenuItem.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        teamMenuItem.setLabel("Team");
        teamMenuItem.setMargin(new java.awt.Insets(0, 23, 0, 0));
        teamMenuItem.setPreferredSize(new java.awt.Dimension(85, 19));

        jMenu1.setText("Add");

        teamBlueAllianceMenuItem.setText("From BlueAlliance");
        teamBlueAllianceMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teamBlueAllianceMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(teamBlueAllianceMenuItem);

        teamEventMenuItem.setText("Teams From Event");
        teamEventMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teamEventMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(teamEventMenuItem);

        teamManualMenuItem.setText("Manually");
        teamManualMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teamManualMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(teamManualMenuItem);

        teamMenuItem.add(jMenu1);

        teamViewMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        teamViewMenuItem.setText("View All");
        teamViewMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teamViewMenuItemActionPerformed(evt);
            }
        });
        teamMenuItem.add(teamViewMenuItem);

        menuBar.add(teamMenuItem);

        eventMenu.setText("Event");
        eventMenu.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        eventMenu.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        eventMenu.setMargin(new java.awt.Insets(0, 23, 0, 0));
        eventMenu.setPreferredSize(new java.awt.Dimension(85, 20));

        jMenu2.setText("Add");

        eventBlueAllianceMenuItem.setText("From BlueAlliance");
        eventBlueAllianceMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventBlueAllianceMenuItemActionPerformed(evt);
            }
        });
        jMenu2.add(eventBlueAllianceMenuItem);

        eventManualMenuItem.setText("Manually");
        eventManualMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventManualMenuItemActionPerformed(evt);
            }
        });
        jMenu2.add(eventManualMenuItem);

        eventMenu.add(jMenu2);

        eventViewAllMenuTiem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        eventViewAllMenuTiem.setText("View All");
        eventViewAllMenuTiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventViewAllMenuTiemActionPerformed(evt);
            }
        });
        eventMenu.add(eventViewAllMenuTiem);

        menuBar.add(eventMenu);

        teamMatchMenu.setText("Team Match");
        teamMatchMenu.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        teamMatchMenu.setMargin(new java.awt.Insets(0, 23, 0, 23));

        jMenu3.setText("Add");

        matchManualMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        matchManualMenuItem.setText("Manually");
        matchManualMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matchManualMenuItemActionPerformed(evt);
            }
        });
        jMenu3.add(matchManualMenuItem);

        teamMatchMenu.add(jMenu3);

        jMenu5.setText("View");

        matchesAllMenuItem.setText("All");
        matchesAllMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matchesAllMenuItemActionPerformed(evt);
            }
        });
        jMenu5.add(matchesAllMenuItem);

        matchByTeamMenuItem.setText("By Team");
        matchByTeamMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matchByTeamMenuItemActionPerformed(evt);
            }
        });
        jMenu5.add(matchByTeamMenuItem);

        matchByEventMenuItem.setText("By Event");
        matchByEventMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matchByEventMenuItemActionPerformed(evt);
            }
        });
        jMenu5.add(matchByEventMenuItem);

        teamMatchMenu.add(jMenu5);

        jMenuItem4.setText("Search");
        teamMatchMenu.add(jMenuItem4);

        menuBar.add(teamMatchMenu);

        eventMatchMenu.setText("Event Match");
        eventMatchMenu.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        eventMatchMenu.setMargin(new java.awt.Insets(0, 23, 0, 23));

        jMenu4.setText("Add");

        eventMatchBlueAllianceMenuItem.setText("From BlueAlliance");
        eventMatchBlueAllianceMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventMatchBlueAllianceMenuItemActionPerformed(evt);
            }
        });
        jMenu4.add(eventMatchBlueAllianceMenuItem);

        eventMatchManualMenuItem.setText("Manually");
        eventMatchManualMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventMatchManualMenuItemActionPerformed(evt);
            }
        });
        jMenu4.add(eventMatchManualMenuItem);

        eventMatchMenu.add(jMenu4);

        jMenu6.setText("View");

        eventMatchAllMenuItem.setText("All");
        eventMatchAllMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventMatchAllMenuItemActionPerformed(evt);
            }
        });
        jMenu6.add(eventMatchAllMenuItem);

        eventMatchByEventMenuItem.setText("By Event");
        eventMatchByEventMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventMatchByEventMenuItemActionPerformed(evt);
            }
        });
        jMenu6.add(eventMatchByEventMenuItem);

        eventMatchMenu.add(jMenu6);

        jMenuItem8.setText("Search");
        eventMatchMenu.add(jMenuItem8);

        menuBar.add(eventMatchMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 599, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void teamBlueAllianceMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teamBlueAllianceMenuItemActionPerformed
        ArrayList<Team> teams = new ArrayList();
        try {
            for (int i = 0; i <= 10; i++) {
                URL oracle = new URL("http://www.thebluealliance.com/api/v2/teams/" + i + "?X-TBA-App-Id=team1100:scouting:v1");
                System.out.println(oracle.openStream());
                Reader jsonReader = new InputStreamReader(oracle.openStream());
                JSONParser jsonParser = new JSONParser();
                JSONArray jsonTeamList = (JSONArray) jsonParser.parse(jsonReader);

                for (Object jsonObject : jsonTeamList) {
                    Team newTeam = new Team();
                    JSONObject jsonEvent = (JSONObject) jsonObject;
                    newTeam.setTeamNumber(((Long) jsonEvent.get("team_number")).intValue());
                    newTeam.setNickname((String) jsonEvent.get("nickname"));
                    newTeam.setLocation((String) jsonEvent.get("location"));
                    newTeam.setKey((String) jsonEvent.get("key"));
                    teams.add(newTeam);
                }

            }
            TeamViewPanel newTeamPanel = new TeamViewPanel(teams, TeamViewPanel.MULTI_SELECT);
            int teamsResult = JOptionPane.showConfirmDialog(new JFrame(), newTeamPanel, "New Teams", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            
            if (teamsResult == JOptionPane.OK_OPTION) {
                String teamNumberErrors = "";
                for (Team team : newTeamPanel.getMultipleTeams()) {
                    if (!TeamHandler.addTeam(team)) {
                        teamNumberErrors += " " + team.getTeamNumber() + ",";
                    }
                }

                if (!teamNumberErrors.equals("")) {
                    JOptionPane.showConfirmDialog(newTeamPanel, "Team(s)" + teamNumberErrors.substring(0, teamNumberErrors.length() - 2) + " already exists in database.", "Warning", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnknownHostException | NoRouteToHostException e) {
            JOptionPane.showConfirmDialog(new JFrame(), "Could not connect to TheBlueAlliance.com", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }


    }//GEN-LAST:event_teamBlueAllianceMenuItemActionPerformed

    private void teamManualMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teamManualMenuItemActionPerformed
        TeamManualPanel manualTeamPanel = new TeamManualPanel();
        boolean newTeamAdded = false;
        do {
            try {
                int addTeamResult = JOptionPane.showConfirmDialog(new JFrame(), manualTeamPanel, "New Team", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

                if (addTeamResult == JOptionPane.OK_OPTION) {
                    if (TeamHandler.addTeam(manualTeamPanel.getTeam())) {
                        newTeamAdded = true;
                    } else {
                        JOptionPane.showConfirmDialog(manualTeamPanel, "Team " + manualTeamPanel.getTeam().getTeamNumber() + " already exists in database.", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    break;
                }

            } catch (NumberFormatException e) {
                JOptionPane.showConfirmDialog(manualTeamPanel, "Please enter a number", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
            }
        } while (!newTeamAdded);

    }//GEN-LAST:event_teamManualMenuItemActionPerformed

    private void teamEventMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teamEventMenuItemActionPerformed
        EventSearchSelectPanel eventSelectPanel = new EventSearchSelectPanel();
        int eventResult = JOptionPane.showConfirmDialog(new JFrame(), eventSelectPanel, "Events", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (eventResult == JOptionPane.OK_OPTION && eventSelectPanel.getSelectedEvent() != null) {
            ArrayList<Team> teams = new ArrayList();
            try {
                URL oracle = new URL("http://www.thebluealliance.com//api/v2/event/" + eventSelectPanel.getSelectedEvent().getKey() + "/teams?X-TBA-App-Id=team1100:scouting:v1");
                System.out.println(oracle.openStream());
                Reader jsonReader = new InputStreamReader(oracle.openStream());
                JSONParser jsonParser = new JSONParser();
                JSONArray jsonTeamList = (JSONArray) jsonParser.parse(jsonReader);

                for (Object jsonObject : jsonTeamList) {
                    Team newTeam = new Team();
                    JSONObject jsosTeam = (JSONObject) jsonObject;
                    newTeam.setTeamNumber(((Long) jsosTeam.get("team_number")).intValue());
                    newTeam.setNickname((String) jsosTeam.get("nickname"));
                    newTeam.setLocation((String) jsosTeam.get("location"));
                    newTeam.setKey((String) jsosTeam.get("key"));
                    teams.add(newTeam);
                }

                TeamViewPanel teamSelectPanel = new TeamViewPanel(teams, TeamViewPanel.MULTI_SELECT);
                int addTeamsResult = JOptionPane.showConfirmDialog(new JFrame(), teamSelectPanel, "Teams from " + eventSelectPanel.getSelectedEvent().getName() , JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

                if (addTeamsResult == JOptionPane.OK_OPTION && teamSelectPanel.getMultipleTeams() != null) {
                    for (Team team : teamSelectPanel.getMultipleTeams()) {
                        if (!TeamHandler.addTeam(team)) {
                            TeamHandler.getTeamByNumnber(team.getTeamNumber()).setKey(team.getKey());
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnknownHostException | NoRouteToHostException e) {
                JOptionPane.showConfirmDialog(new JFrame(), "Could not connect to TheBlueAlliance.com", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }

        }
    }//GEN-LAST:event_teamEventMenuItemActionPerformed

    private void eventBlueAllianceMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventBlueAllianceMenuItemActionPerformed
        EventSearchSelectPanel eventSelectPanel = new EventSearchSelectPanel();
        int addEventResult = JOptionPane.showConfirmDialog(new JFrame(), eventSelectPanel, "New Event", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (addEventResult == JOptionPane.OK_OPTION && eventSelectPanel.getSelectedEvent() != null) {
            EventHandler.addEvent(eventSelectPanel.getSelectedEvent());
        }
    }//GEN-LAST:event_eventBlueAllianceMenuItemActionPerformed

    private void eventManualMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventManualMenuItemActionPerformed
        EventManualPanel eventManualPanel = new EventManualPanel();
        int addEventResult = JOptionPane.showConfirmDialog(new JFrame(), eventManualPanel, "New Event", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (addEventResult == JOptionPane.OK_OPTION) {
            EventHandler.addEvent(eventManualPanel.getEvent());
        }
    }//GEN-LAST:event_eventManualMenuItemActionPerformed

    private void matchManualMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matchManualMenuItemActionPerformed
        MatchPanel matchPanel = new MatchPanel();
        boolean newMatchAdded = false;

        do {
            int newMatchResult = JOptionPane.showConfirmDialog(new JFrame(), matchPanel, "New Match", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

            if (newMatchResult == JOptionPane.OK_OPTION) {
                if (TeamEventMatchHandler.addMatch(matchPanel.getMatch())) {
                    newMatchAdded = true;
                } else {
                    JOptionPane.showConfirmDialog(matchPanel, "Match " + matchPanel.getMatch().getMatchNumber() + " for team " + matchPanel.getMatch().getTeamNumber() + " already exists in the database.", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
                }

            } else {
                break;
            }

        } while (!newMatchAdded);
    }//GEN-LAST:event_matchManualMenuItemActionPerformed

    private void eventMatchBlueAllianceMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventMatchBlueAllianceMenuItemActionPerformed
        EventSearchSelectPanel eventSelect = new EventSearchSelectPanel();
        int eventsResult = JOptionPane.showConfirmDialog(new JFrame(), eventSelect, "Events", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

        if (eventsResult == JOptionPane.OK_OPTION && eventSelect.getSelectedEvent() != null) {
            ArrayList<EventMatch> eventMatches = new ArrayList();
            try {
                URL oracle = new URL("http://www.thebluealliance.com/api/v2/event/" + eventSelect.getSelectedEvent().getKey() + "/matches?X-TBA-App-Id=team1100:scouting:v1");
                System.out.println(oracle.openStream());
                Reader jsonReader = new InputStreamReader(oracle.openStream());

                JSONParser jsonParser = new JSONParser();
                JSONArray jsonEventMatchesArray = (JSONArray) jsonParser.parse(jsonReader);

                for (Object o : jsonEventMatchesArray) {
                    EventMatch newEventMatch = new EventMatch();
                    JSONObject jsonEventMatch = (JSONObject) o;
                    newEventMatch.setLevel((String) jsonEventMatch.get("comp_level"));
                    newEventMatch.setMatchNumber(((Long) jsonEventMatch.get("match_number")).intValue());

                    JSONObject jsonAlliance = (JSONObject) jsonEventMatch.get("alliances");
                    JSONObject jsonBlue = (JSONObject) jsonAlliance.get("blue");

                    newEventMatch.setBlueScore(((Long) jsonBlue.get("score")).intValue());
                    JSONArray jsonBlueTeams = (JSONArray) jsonBlue.get("teams");
                    String blue1 = ((String) jsonBlueTeams.get(0)).substring(3);
                    String blue2 = ((String) jsonBlueTeams.get(1)).substring(3);
                    String blue3 = ((String) jsonBlueTeams.get(2)).substring(3);
                    newEventMatch.setBlue1(Integer.parseInt(blue1));
                    newEventMatch.setBlue2(Integer.parseInt(blue2));
                    newEventMatch.setBlue3(Integer.parseInt(blue3));

                    JSONObject jsonRed = (JSONObject) jsonAlliance.get("red");
                    newEventMatch.setRedScore(((Long) jsonRed.get("score")).intValue());
                    JSONArray jsonRedTeams = (JSONArray) jsonRed.get("teams");
                    String red1 = ((String) jsonRedTeams.get(0)).substring(3);
                    String red2 = ((String) jsonRedTeams.get(1)).substring(3);
                    String red3 = ((String) jsonRedTeams.get(2)).substring(3);
                    newEventMatch.setRed1(Integer.parseInt(red1));
                    newEventMatch.setRed2(Integer.parseInt(red2));
                    newEventMatch.setRed3(Integer.parseInt(red3));

                    eventMatches.add(newEventMatch);
                }

            } catch (UnknownHostException | NoRouteToHostException e) {
                JOptionPane.showConfirmDialog(eventSelect, "Could not connect to TheBlueAlliance.com", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
            } catch (ParseException | IOException e) {
                e.printStackTrace();
            }

            EventMatchViewPanel eventMatchPanel = new EventMatchViewPanel(eventMatches);
            int eventMatchesResult = JOptionPane.showConfirmDialog(new JFrame(), eventMatchPanel, "New Event Match", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            int overlappingEventMatches = 0;

            if (eventMatchesResult == JOptionPane.OK_OPTION) {
                for (EventMatch eventMatch : eventMatchPanel.getMultipleEventMatches()) {
                    if (!EventMatchHandler.addEventMatch(eventMatch)) {
                        overlappingEventMatches++;
                    }
                }
            }

            if (overlappingEventMatches > 0) {
                JOptionPane.showConfirmDialog(new JFrame(), "There were " + overlappingEventMatches + " event matches in the database already.", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_eventMatchBlueAllianceMenuItemActionPerformed

    private void eventMatchManualMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventMatchManualMenuItemActionPerformed
        if (EventHandler.getEvents() != null) {
            EventMatchManualPanel eventMatchPanel = new EventMatchManualPanel(EventHandler.getEvents());
            boolean eventMatchAdded = false;
            do {
                int eventMatchResult = JOptionPane.showConfirmDialog(new JFrame(), eventMatchPanel, "New Event Match", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

                if (eventMatchResult == JOptionPane.OK_OPTION) {
                    try {
                        EventMatchHandler.addEventMatch(eventMatchPanel.getEventMatch());
                        eventMatchAdded = true;
                    } catch (NumberFormatException e) {
                        JOptionPane.showConfirmDialog(new JFrame(), "Please make sure that all values are intereger.", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    break;
                }
            } while (!eventMatchAdded);
        } else {
            JOptionPane.showConfirmDialog(new JFrame(), "There are no events to add a match to", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_eventMatchManualMenuItemActionPerformed

    private void matchesAllMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matchesAllMenuItemActionPerformed
        newJFrame(new MatchViewPanel(TeamEventMatchHandler.getMatches()), "Matches");
    }//GEN-LAST:event_matchesAllMenuItemActionPerformed

    private void matchByTeamMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matchByTeamMenuItemActionPerformed
        TeamViewPanel teamSelect = new TeamViewPanel(TeamHandler.getTeams(), TeamViewPanel.SINGLE_SELECT);
        boolean teamNumber = false;
        do {
            try {
                int eventMatchResult = JOptionPane.showConfirmDialog(new JFrame(), teamSelect, "Select Team", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                if (eventMatchResult == JOptionPane.OK_OPTION && teamSelect.getSingleTeam() != null) {
                    newJFrame(new MatchViewPanel(TeamEventMatchHandler.getMatchesFromTeam(teamSelect.getSingleTeam().getTeamNumber())), "Team Matches");
                    teamNumber = true;
                } else {
                    break;
                }
            } catch (NumberFormatException e) {
                JOptionPane.showConfirmDialog(new JFrame(), "Please enter a number", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
            }
        } while (!teamNumber);


    }//GEN-LAST:event_matchByTeamMenuItemActionPerformed

    private void teamViewMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teamViewMenuItemActionPerformed
        newJFrame(new TeamViewPanel(TeamHandler.getTeams(), TeamViewPanel.EDIT), "Teams");
    }//GEN-LAST:event_teamViewMenuItemActionPerformed

    private void matchByEventMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matchByEventMenuItemActionPerformed
        EventViewPanel eventSelect = new EventViewPanel(EventHandler.getEvents());
        boolean teamNumber = false;
        do {
            try {
                int eventMatchResult = JOptionPane.showConfirmDialog(new JFrame(), eventSelect, "Select Team", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                if (eventMatchResult == JOptionPane.OK_OPTION && eventSelect.getSelectedEvent() != null) {
                    newJFrame(new MatchViewPanel(TeamEventMatchHandler.getMatchesFromEvent(eventSelect.getSelectedEvent())), "Matches");
                    teamNumber = true;
                } else {
                    break;
                }
            } catch (NumberFormatException e) {
                JOptionPane.showConfirmDialog(new JFrame(), "Please enter a number", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
            }
        } while (!teamNumber);
    }//GEN-LAST:event_matchByEventMenuItemActionPerformed

    private void eventMatchAllMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventMatchAllMenuItemActionPerformed
        newJFrame(new EventMatchViewPanel(EventMatchHandler.getEventMatches()), "Event Matches");
    }//GEN-LAST:event_eventMatchAllMenuItemActionPerformed

    private void eventMatchByEventMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventMatchByEventMenuItemActionPerformed
        EventViewPanel eventSelect = new EventViewPanel(EventHandler.getEvents());
        boolean event = false;
        do {
            try {
                int eventMatchResult = JOptionPane.showConfirmDialog(new JFrame(), eventSelect, "Select Event", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                if (eventMatchResult == JOptionPane.OK_OPTION && eventSelect.getSelectedEvent() != null) {
                    newJFrame(new EventMatchViewPanel(EventMatchHandler.getEventMatchesFromEvent(eventSelect.getSelectedEvent())), "Event Matches");
                    event = true;
                } else {
                    break;
                }
            } catch (NumberFormatException e) {
                JOptionPane.showConfirmDialog(new JFrame(), "Please enter a number", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
            }
        } while (!event);
    }//GEN-LAST:event_eventMatchByEventMenuItemActionPerformed

    private void eventViewAllMenuTiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventViewAllMenuTiemActionPerformed
        newJFrame(new EventViewPanel(EventHandler.getEvents()), "Events");
    }//GEN-LAST:event_eventViewAllMenuTiemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem eventBlueAllianceMenuItem;
    private javax.swing.JMenuItem eventManualMenuItem;
    private javax.swing.JMenuItem eventMatchAllMenuItem;
    private javax.swing.JMenuItem eventMatchBlueAllianceMenuItem;
    private javax.swing.JMenuItem eventMatchByEventMenuItem;
    private javax.swing.JMenuItem eventMatchManualMenuItem;
    private javax.swing.JMenu eventMatchMenu;
    private javax.swing.JMenu eventMenu;
    private javax.swing.JMenuItem eventViewAllMenuTiem;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem matchByEventMenuItem;
    private javax.swing.JMenuItem matchByTeamMenuItem;
    private javax.swing.JMenuItem matchManualMenuItem;
    private javax.swing.JMenuItem matchesAllMenuItem;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem teamBlueAllianceMenuItem;
    private javax.swing.JMenuItem teamEventMenuItem;
    private javax.swing.JMenuItem teamManualMenuItem;
    private javax.swing.JMenu teamMatchMenu;
    private javax.swing.JMenu teamMenuItem;
    private javax.swing.JMenuItem teamViewMenuItem;
    // End of variables declaration//GEN-END:variables

    public void display() {
        setVisible(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize((int) screenSize.getWidth(), 60);
    }
    
    private void newJFrame(JPanel panel, String title){
        JFrame jframe = new JFrame();
        jframe.add(panel);
        jframe.pack();
        jframe.setTitle(title);

        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        jframe.setLocation(dim.width/2-jframe.getSize().width/2, dim.height/2-jframe.getSize().height/2);
        
        jframe.setVisible(true);
    }
}
